classdef compare_linear_prediction_reality < experiment
    
    properties
    end
    
    methods
          
        function test(obj,training_policy,training_duration,choice_policy,choice_duration,choice)
            system_=system_base();
            % mpc=mpc_policy(system_);
            system=system_base();
            workload = system.initialize(training_duration+choice_duration);
            res = system.run_plan(system, training_duration,  training_policy);
            past_util = system.past_util();
            
            start_=1;
            if (system.t-1>60)
                start_=system.t-60;
            end
            util_interval =  past_util((start_:end),:);
            u=UtilityLib(); figure;
            subplot(2,1,1);   plot((start_:system.t-1)', util_interval, u.color{1}); hold on;
            %             inst =  system.past_future_inst();
            %             subplot(2,1,2); plot((start_:system.t+60-1)', inst(start_:system.t+60-1,:));
            
            % do estimation -------------------
            model=linear_model(); 
            model.build_model( system.past_inst(), system.past_util());
            prediction = mpc_policy(model).predict_choice_outcome(choice,  system.past_future_inst(), system.past_util(), system.t-1 );
            subplot(2,1,1); plot((system.t-1:system.t+60-1)', prediction.util,  u.color{2});
            % subplot(3,1,2); plot((start_:system.t+60-1)', prediction.inst);          
            
            max_util=max(max(prediction.util),max(system.past_util()));
            subplot(2,1,1); line([system.t system.t],[0 max_util],...
                'Color',u.color{1},...
                'LineStyle',u.lnstyle{1},...
                'LineWidth',2,...
                'Marker','s',...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',6);
            
            
            % continue experiment -------------------
%             choice_plan=@()[1,   1,  1, 1];
%             s=static_policy(choice_plan);            
            system.run_plan(system, choice_duration , choice_policy);
            subplot(2,1,1); plot((start_:system.t-1)', system.past_util(),  u.color{1});
             max_util=max(max(prediction.util),max(system.past_util()));
             axis([0 system.t 0 max_util]);      
            inst =  system.past_future_inst();
            subplot(2,1,2); plot((start_:system.t-1)', inst(start_:system.t-1,:));
            
            % plot((system.t-60:system.t)', res.util_est);
            %               subplot(2,1,2); plot((system.t-60:system.t+60)',res.inst);
            %                 subplot(4,1,3); plot( system.past_future_inst()  );
            %                 subplot(4,1,4); plot(past_util); hold on; plot((system.t-60:system.t+60)',yp);
        end
        
        function run_test(obj)
            p1=  static_policy(@()...  
                [1, 2, 2, 1 %fire 1 instance of type 1 at min 1
                 5, 2, 1, 0
                 14,1, 1, 1
                 28,1, 2, 1
                 34,1, 2, 1]); 
            
             p0=static_policy(@()...
                [1,  1, 3, 1
                 4,  2, 2, 1
                 5,  1, 2, 0
                 9,  3, 7, 1
                12, 2, 2, 0
                14, 4, 4, 1
                17, 3, 5, 0
                18, 5, 1, 1
                21, 4, 4, 0
                24, 6, 5, 1
                28, 5, 1, 0
                30, 7, 1, 1
                35, 6, 5, 0
                35, 8, 2, 1
                37, 7, 1, 0                
                41, 9, 1, 1
                45, 8, 2, 0
                49,10, 2, 1
                50, 9, 1,  0
                52. 7, 1, 0
                54,11, 1, 1]); 
             
            p2=static_policy(@()[1,   3,  2, 1]);
            choice=[0 0 2 0 0 0 0 0 0 0 0 ];
            obj.test(p0,60,p2,60,choice)
        end
            
    end
    
end

